Passed
Push — master ( 3158aa...35523f )
by EMP
01:18
created

main.js ➔ getCountryFlag   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
1
"use strict";
2
3
sodium.ready.then(function() {
1 ignored issue
show
Bug introduced by
The variable sodium seems to be never declared. If this is a global, consider adding a /** global: sodium */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
4
5
const ae = new AllEars(function(ok) {
1 ignored issue
show
Bug introduced by
The variable AllEars seems to be never declared. If this is a global, consider adding a /** global: AllEars */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
6
	if (ok) {
7
		document.getElementById("txt_skey").style.background = "#466";
8
		document.getElementById("txt_skey").maxLength = "64";
9
	} else {
10
		console.log("Failed to load All-Ears");
11
	}
12
});
13
14
function TabState(cur, max, btnDele, btnUpdt) {
15
	this.cur = cur;
16
	this.max = max;
17
	this.btnDele = btnDele;
18
	this.btnUpdt = btnUpdt;
19
}
20
21
const tabs = [
22
	new TabState(0, 0, false, true), // Inbox
23
	new TabState(0, 0, false, true), // Outbx
24
	new TabState(0, 2, true, false), // Write
25
	new TabState(0, 0, false, true), // Notes
26
	new TabState(0, 0, false, false) // Admin
27
];
28
29
let tab = 0;
30
const TAB_INBOX = 0;
31
const TAB_OUTBX = 1;
0 ignored issues
show
Unused Code introduced by
The constant TAB_OUTBX seems to be never used. Consider removing it.
Loading history...
32
const TAB_WRITE = 2;
33
const TAB_NOTES = 3;
0 ignored issues
show
Unused Code introduced by
The constant TAB_NOTES seems to be never used. Consider removing it.
Loading history...
34
const TAB_ADMIN = 4;
0 ignored issues
show
Unused Code introduced by
The constant TAB_ADMIN seems to be never used. Consider removing it.
Loading history...
35
36
// Helper functions
37
function getCountryName(countryCode) {
38
	const opts = document.getElementById("gatekeeper_country");
39
40
	for (let i = 0; i < opts.length; i++) {
41
		if (opts[i].value === countryCode) {
42
			return opts[i].textContent;
43
		}
44
	}
45
46
	return "Unknown countrycode: " + countryCode;
47
}
48
49
function getCountryFlag(countryCode) {
50
	return sodium.to_string(new Uint8Array([
1 ignored issue
show
Bug introduced by
The variable sodium seems to be never declared. If this is a global, consider adding a /** global: sodium */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
51
		240, 159, 135, 166 + countryCode.codePointAt(0) - 65,
52
		240, 159, 135, 166 + countryCode.codePointAt(1) - 65
53
	]));
54
}
55
56
function getMsgId(num) {
57
	let i;
58
	if (ae.GetExtMsgHeaders(num).toLowerCase().slice(0, 11) === "message-id:") {
59
		i = 0;
60
	} else {
61
		i = ae.GetExtMsgHeaders(num).toLowerCase().indexOf("\nmessage-id:");
62
		if (i < 1) return "ERR";
63
		i++;
64
	}
65
66
	const x = ae.GetExtMsgHeaders(num).slice(i + 11).trim();
67
	if (x[0] !== "<") return "ERR2";
68
	return x.slice(1, x.indexOf(">"));
69
}
70
71
function displayMsg(isInt, num) {
72
	document.getElementById("midright").scroll(0, 0);
73
74
	const ts = isInt? ae.GetIntMsgTime(num) : ae.GetExtMsgTime(num);
75
76
	document.getElementById("btn_reply").disabled = false;
77
	document.getElementById("btn_reply").onclick = function() {
78
		document.getElementById("write_recv").value = isInt? ae.GetIntMsgFrom(num) : ae.GetExtMsgFrom(num);
79
		document.getElementById("write_subj").value = "Re: " + (isInt ? ae.GetIntMsgTitle(num) : ae.GetExtMsgTitle(num));
80
		document.getElementById("write_rply").textContent = (isInt? "" : getMsgId(num));
81
		document.getElementById("btn_write").click();
82
		document.getElementById("div_write_1").hidden = false;
83
		document.getElementById("div_write_2").hidden = true;
84
		document.getElementById("write_body").focus();
85
		for (const opt of document.getElementById("write_from").options) {
86
			if (opt.value === (isInt ? ae.GetIntMsgTo(num) : ae.GetExtMsgTo(num))) {
87
				opt.selected = true;
88
			}
89
		}
90
	};
91
92
	document.getElementById("msg").hidden = false;
93
	document.getElementById("msg").getElementsByTagName("h1")[0].textContent = isInt ? ae.GetIntMsgTitle(num) : ae.GetExtMsgTitle(num);
94
	document.getElementById("msg").getElementsByTagName("pre")[0].textContent = isInt ? ae.GetIntMsgBody(num) : ae.GetExtMsgBody(num);
95
96
	document.getElementById("readmsg_to").textContent = isInt ? ae.GetIntMsgTo(num) : ae.GetExtMsgTo(num);
97
	document.getElementById("readmsg_date").children[0].textContent = new Date(ts * 1000).toISOString().slice(0, 19).replace("T", " ");
98
99
	if (!isInt) {
100
		document.getElementById("readmsg_ip").hidden = false;
101
		document.getElementById("readmsg_country").hidden = false;
102
		document.getElementById("readmsg_tls").hidden = false;
103
		document.getElementById("readmsg_greet").hidden = false;
104
		document.getElementById("readmsg_timing").hidden = false;
105
		document.getElementById("readmsg_envfrom").hidden = false;
106
107
		const cc = ae.GetExtMsgCountry(num);
108
109
		document.getElementById("readmsg_ip").children[0].textContent = ae.GetExtMsgIp(num);
110
		document.getElementById("readmsg_country").textContent = getCountryFlag(cc) + " " + getCountryName(cc);
111
		document.getElementById("readmsg_tls").children[0].textContent = ae.GetExtMsgTLS(num);
112
		document.getElementById("readmsg_greet").children[0].textContent = ae.GetExtMsgGreet(num);
113
		document.getElementById("readmsg_envfrom").textContent = ae.GetExtMsgFrom(num);
114
115
		let flagText = "";
116
		if (!ae.GetExtMsgFlagVPad(num)) flagText += "<abbr title=\"Invalid padding\">PAD</abbr> ";
117
		if (!ae.GetExtMsgFlagVSig(num)) flagText += "<abbr title=\"Invalid signature\">SIG</abbr> ";
118
		if (!ae.GetExtMsgFlagPExt(num)) flagText += "<abbr title=\"The sender did not use the Extended (ESMTP) protocol\">SMTP</abbr> ";
119
		if (!ae.GetExtMsgFlagQuit(num)) flagText += "<abbr title=\"The sender did not issue the required QUIT command\">QUIT</abbr> ";
120
		if (ae.GetExtMsgFlagRare(num)) flagText += "<abbr title=\"The sender issued unusual command(s)\">RARE</abbr> ";
121
		if (ae.GetExtMsgFlagFail(num)) flagText += "<abbr title=\"The sender issued invalid command(s)\">FAIL</abbr> ";
122
		if (ae.GetExtMsgFlagPErr(num)) flagText += "<abbr title=\"The sender violated the protocol\">PROT</abbr> ";
123
		document.getElementById("readmsg_flags").children[0].innerHTML = flagText.trim();
124
	} else {
125
		document.getElementById("readmsg_ip").hidden = true;
126
		document.getElementById("readmsg_country").hidden = true;
127
		document.getElementById("readmsg_tls").hidden = true;
128
		document.getElementById("readmsg_greet").hidden = true;
129
		document.getElementById("readmsg_timing").hidden = true;
130
		document.getElementById("readmsg_envfrom").hidden = true;
131
132
		let symbol = "<span title=\"Invalid level\">&#x26a0;</span>";
133
		if (ae.GetIntMsgFrom(num) === "system") {if (ae.GetIntMsgLevel(num) === 3) symbol = "<span title=\"System\">&#x1f162;</span>";} // S (System)
134
		else if (ae.GetIntMsgLevel(num) === 0) symbol = "<span title=\"Level 0 User\">&#x1f10c;</span>"; // 0
135
		else if (ae.GetIntMsgLevel(num) === 1) symbol = "<span title=\"Level 1 User\">&#x278a;</span>"; // 1
136
		else if (ae.GetIntMsgLevel(num) === 2) symbol = "<span title=\"Level 2 User\">&#x278b;</span>"; // 2
137
		else if (ae.GetIntMsgLevel(num) === 3) symbol = "<span title=\"Administrator\">&#x1f150;</span>"; // A (Admin)
138
		document.getElementById("readmsg_from").innerHTML = symbol + " " + ae.GetIntMsgFrom(num);
139
140
		let flagText = "";
141
		if (!ae.GetIntMsgFlagVPad(num)) flagText += "<abbr title=\"Invalid padding\">PAD</abbr> ";
142
		if (!ae.GetIntMsgFlagVSig(num)) flagText += "<abbr title=\"Invalid signature\">SIG</abbr> ";
143
		document.getElementById("readmsg_flags").children[0].innerHTML = flagText.trim();
144
	}
145
}
146
147
// Interface
148
function addMsg(isInt, i) {
149
	const inbox = document.getElementById("tbl_inbox");
150
	const sent = document.getElementById("tbl_outbx");
151
152
	const isSent = false; //TODO
153
	const table = isSent ? sent : inbox;
154
155
	const row = table.insertRow(-1);
156
	const cellTime = row.insertCell(-1);
157
	const cellSubj = row.insertCell(-1);
158
	const cellSnd1 = row.insertCell(-1);
159
	const cellSnd2 = row.insertCell(-1);
160
161
	const ts = isInt? ae.GetIntMsgTime(i) : ae.GetExtMsgTime(i);
162
	cellTime.setAttribute("data-ts", ts);
163
	cellTime.textContent = new Date(ts * 1000).toISOString().slice(0, 10);
164
165
	cellSubj.textContent = isInt? ae.GetIntMsgTitle(i) : ae.GetExtMsgTitle(i);
166
167
	if (isInt) {
168
		cellSnd1.textContent = ae.GetIntMsgFrom(i);
169
		cellSnd1.className = (ae.GetIntMsgFrom(i).length === 16) ? "mono" : "";
170
	} else {
171
		const from1 = ae.GetExtMsgFrom(i);
172
		const from2 = from1.substring(from1.indexOf("@") + 1);
173
		const cc = ae.GetExtMsgCountry(i);
174
175
		cellSnd1.textContent = from1.substring(0, from1.indexOf("@"));
176
177
		const flag = document.createElement("abbr");
178
		flag.textContent = getCountryFlag(cc);
179
		flag.title = getCountryName(cc);
180
		cellSnd2.appendChild(flag);
181
182
		const fromText = document.createElement("span");
183
		fromText.textContent = " " + from2;
184
		cellSnd2.appendChild(fromText);
185
	}
186
187
//	divDel.innerHTML = "<input class=\"delMsg\" type=\"checkbox\" data-id=\"" + ae.GetIntMsgIdHex(i) + "\">";
188
189
	row.onclick = function() {
190
		displayMsg(isInt, i);
191
	};
192
/*
193
	cellDel.children[0].onchange = function() {
194
		if (!divDel.children[0].checked) {
195
			const checkboxes = elmt.getElementsByTagName("input");
196
			let checked = false;
197
198
			for (let j = 0; j < checkboxes.length; j++) {
199
				if (checkboxes[j].checked) {
200
					checked = true;
201
					break;
202
				}
203
			}
204
205
			if (!checked) {
206
				document.getElementById(isSent ? "btn_sentdel" : "btn_msgdel").hidden = true;
207
				return;
208
			}
209
		}
210
211
		document.getElementById(isSent? "btn_sentdel" : "btn_msgdel").hidden = false;
212
	};
213
*/
214
}
215
216
function getRowsPerPage() {
217
	const tbl = document.getElementById("tbl_inbox");
218
	tbl.innerHTML = "";
219
	const row = tbl.insertRow(-1);
220
	const cell = row.insertCell(-1);
221
	cell.textContent = "0";
222
223
	const rowsPerPage = Math.floor(getComputedStyle(document.getElementById("div_inbox")).height.replace("px", "") / getComputedStyle(document.querySelector("#tbl_inbox > tbody > tr:first-child")).height.replace("px", "")) - 1; // -1 allows space for 'load more'
224
	tbl.innerHTML = "";
225
	return rowsPerPage;
226
}
227
228
function addMessages() {
229
	const rowsPerPage = getRowsPerPage();
230
	let skipMsgs = rowsPerPage * tabs[TAB_INBOX].cur;
231
232
	const maxExt = ae.GetExtMsgCount();
233
	const maxInt = ae.GetIntMsgCount();
234
235
	tabs[TAB_INBOX].max = Math.floor((maxExt + maxInt) / rowsPerPage);
236
237
	let numExt = 0;
238
	let numInt = 0;
239
	let numAdd = 0;
240
241
	while (numAdd < rowsPerPage) {
242
		const tsInt = (numInt < maxInt) ? ae.GetIntMsgTime(numInt) : -1;
243
		const tsExt = (numExt < maxExt) ? ae.GetExtMsgTime(numExt) : -1;
244
		if (tsInt === -1 && tsExt === -1) break;
245
246
		if (tsInt !== -1 && (tsExt === -1 || tsInt > tsExt)) {
247
			if (skipMsgs > 0) skipMsgs--; else {addMsg(true, numInt); numAdd++;}
248
			numInt++;
249
		} else if (tsExt !== -1) {
250
			if (skipMsgs > 0) skipMsgs--; else {addMsg(false, numExt); numAdd++;}
251
			numExt++;
252
		}
253
	}
254
255
	if (ae.GetReadyMsgKilos() < ae.GetTotalMsgKilos()) {
256
		const inbox = document.getElementById("tbl_inbox");
257
		const row = inbox.insertRow(-1);
258
		const cell = row.insertCell(-1);
259
		cell.textContent = "Load more (" + (ae.GetTotalMsgKilos() - ae.GetReadyMsgKilos()) + " KiB left)";
260
261
		row.onclick = function() {
262
			this.onclick = "";
263
264
			ae.Message_Browse(false, function(successBrowse) {
265
				document.getElementById("tbl_inbox").style.opacity = 1;
266
267
				if (successBrowse) {
268
					addMessages();
269
					if (tabs[tab].cur < tabs[tab].max) document.getElementById("btn_rght").disabled = false;
270
				}
271
			});
272
		};
273
	}
274
}
275
276
function updateAddressCounts() {
277
	document.getElementById("limit_normal").textContent = (ae.GetAddressCountNormal() + "/" + ae.GetAddressLimitNormal(ae.GetUserLevel())).padStart(ae.GetAddressLimitNormal(ae.GetUserLevel()) > 9 ? 5 : 1);
278
	document.getElementById("limit_shield").textContent = (ae.GetAddressCountShield() + "/" + ae.GetAddressLimitShield(ae.GetUserLevel())).padStart(ae.GetAddressLimitShield(ae.GetUserLevel()) > 9 ? 5 : 1);
279
	document.getElementById("limit_total").textContent = ((ae.GetAddressCountNormal() + ae.GetAddressCountShield()) + "/" + ae.GetAddrPerUser()).padStart(5);
280
}
281
282
function reloadInterface() {
283
	document.getElementById("div_begin").hidden = true;
284
	document.getElementById("div_main").style.display = "grid";
285
286
	// Contacts
287
	for (let i = 0; i < ae.GetContactCount(); i++) {
288
		addContact(
289
			ae.GetContactMail(i),
290
			ae.GetContactName(i),
291
			ae.GetContactNote(i)
292
		);
293
	}
294
295
	// Addresses
296
	for (let i = 0; i < ae.GetAddressCount(); i++) {
297
		addAddress(i);
298
	}
299
300
	document.getElementById("table_addrs").getElementsByTagName("caption")[0].textContent = "Level " + ae.GetUserLevel() + " User";
301
	updateAddressCounts();
302
}
303
304 View Code Duplication
function deleteAddress(addr) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
305
	let btns = document.getElementById("tbl_addrs").getElementsByTagName("button");
306
	for (let i = 0; i < btns.length; i++) btns[i].disabled = true;
307
308
	let addressToDelete = -1;
309
310
	for (let i = 0; i < ae.GetAddressCount(); i++) {
311
		if (addr === ae.GetAddress(i)) {
312
			addressToDelete = i;
313
			break;
314
		}
315
	}
316
317
	if (addressToDelete === -1) return;
318
319
	ae.Address_Delete(addressToDelete, function(success) {
320
		if (success) {
321
			document.getElementById("tbl_addrs").deleteRow(addressToDelete);
322
			document.getElementById("write_from").remove(addressToDelete);
323
			updateAddressCounts();
324
325
			if (ae.GetAddressCountNormal() < ae.GetAddressLimitNormal(ae.GetUserLevel())) document.getElementById("btn_address_create_normal").disabled = false;
326
			if (ae.GetAddressCountShield() < ae.GetAddressLimitShield(ae.GetUserLevel())) document.getElementById("btn_address_create_shield").disabled = false;
327
328
			ae.Private_Update(function(success2) {
329
				if (!success2) console.log("Failed to update the Private field");
330
331
				btns = document.getElementById("tbl_addrs").getElementsByTagName("button");
332
				for (let i = 0; i < btns.length; i++) btns[i].disabled = false;
333
			});
334
		} else {
335
			console.log("Failed to delete address");
336
337
			btns = document.getElementById("tbl_addrs").getElementsByTagName("button");
338
			for (let i = 0; i < btns.length; i++) btns[i].disabled = false;
339
		}
340
	});
341
}
342
343 View Code Duplication
function shieldMix(addr) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
344
	let newAddr = "";
345
346
	for (let i = 0; i < 16; i++) {
347
		switch (addr.charAt(i)) {
348
			case '1':
349
				newAddr += "1iIlL".charAt(Math.floor(Math.random() * 5));
350
				break;
351
			case '0':
352
				newAddr += "0oO".charAt(Math.floor(Math.random() * 3));
353
				break;
354
			case 'w':
355
				newAddr += "VvWw".charAt(Math.floor(Math.random() * 4));
356
				break;
357
			default:
358
				newAddr += (Math.random() > 0.5) ? addr.charAt(i) : addr.charAt(i).toUpperCase();
359
		}
360
	}
361
362
	return newAddr;
363
}
364
365
function addAddress(num) {
366
	const addrTable = document.getElementById("tbl_addrs");
367
	const row = addrTable.insertRow(-1);
368
	const cellAddr = row.insertCell(-1);
369
	const cellChk1 = row.insertCell(-1);
370
	const cellChk2 = row.insertCell(-1);
371
	const cellChk3 = row.insertCell(-1);
372
	const cellBtnD = row.insertCell(-1);
373
374
	cellAddr.textContent = ae.GetAddress(num);
375
	cellAddr.onclick = function() {
376
		if (cellAddr.textContent.length === 16)
377
			navigator.clipboard.writeText(shieldMix(cellAddr.textContent) + "@" + ae.GetDomainEml());
1 ignored issue
show
Bug introduced by
The variable navigator seems to be never declared. If this is a global, consider adding a /** global: navigator */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
378
		else
379
			navigator.clipboard.writeText(cellAddr.textContent + "@" + ae.GetDomainEml());
380
	};
381
382
	cellChk1.innerHTML = ae.GetAddressAccExt(num) ? "<input type=\"checkbox\" checked=\"checked\">" : "<input type=\"checkbox\">";
383
	cellChk2.innerHTML = ae.GetAddressAccInt(num) ? "<input type=\"checkbox\" checked=\"checked\">" : "<input type=\"checkbox\">";
384
	cellChk3.innerHTML = ae.GetAddressUse_Gk(num) ? "<input type=\"checkbox\" checked=\"checked\">" : "<input type=\"checkbox\">";
385
386
	cellBtnD.innerHTML = "<button type=\"button\">X</button>";
387
	cellBtnD.onclick = function() {deleteAddress(cellAddr.textContent);};
388
389
	const opt = document.createElement("option");
390
	opt.value = cellAddr.textContent;
391
	opt.textContent = cellAddr.textContent + "@" + ae.GetDomainEml();
392
	document.getElementById("write_from").appendChild(opt);
393
}
394
395
document.getElementById("btn_updt").onclick = function() {
396
	const btn = this;
397
	btn.disabled = true;
398
	btn.blur();
399
400
	if (tab === TAB_INBOX) {
401
		document.getElementById("tbl_inbox").style.opacity = 0.5;
402
403
		ae.Message_Browse(true, function(successBrowse) {
404
			document.getElementById("tbl_inbox").style.opacity = 1;
405
406
			if (successBrowse) {
407
				addMessages();
408
				btn.disabled = false;
409
			} else {
410
				console.log("Failed to refresh");
411
				btn.disabled = false;
412
			}
413
		});
414
	}
415
};
416
417
function addContact(mail, name, note) {
418
	const tbl = document.getElementById("tbl_ctact");
419
	const row = tbl.insertRow(-1);
420
	const cellMail = row.insertCell(-1);
421
	const cellName = row.insertCell(-1);
422
	const cellNote = row.insertCell(-1);
423
	const cellBtnD = row.insertCell(-1);
424
425
	cellMail.textContent = mail;
426
	cellName.textContent = name;
427
	cellNote.textContent = note;
428
	cellBtnD.innerHTML = "<button type=\"button\">X</button>";
429
430
	cellMail.contentEditable = true;
431
	cellName.contentEditable = true;
432
	cellNote.contentEditable = true;
433
434
	cellBtnD.onclick = function() {row.remove();};
435
}
436
437
document.getElementById("btn_newcontact").onclick = function() {
438
	addContact("", "", "");
439
};
440
441
document.getElementById("btn_savecontacts").onclick = function() {
442
	while (ae.GetContactCount() > 0) {
443
		ae.DeleteContact(0);
444
	}
445
446
	for (const row of document.getElementById("tbl_ctact").rows) {
447
		ae.AddContact(row.cells[0].textContent, row.cells[1].textContent, row.cells[2].textContent);
448
	}
449
450
	const btn = this;
451
	btn.disabled = true;
452
453
	ae.Private_Update(function(success) {
454
		btn.disabled = false;
455
456
		if (!success) {
457
			console.log("Failed contacts update");
458
		}
459
	});
460
};
461
462
function updateTab() {
463
	switch (tab) {
464
		case TAB_INBOX:
465
			addMessages();
466
		break;
467
468
		case TAB_WRITE:
469
			switch (tabs[tab].cur) {
470
				case 0:
471
					document.getElementById("div_write_1").hidden = false;
472
					document.getElementById("div_write_2").hidden = true;
473
					document.getElementById("write_body").focus();
474
				break;
475
476
				case 1:
477
					ae.Address_Lookup(document.getElementById("write_recv").value, function(pk) {
478
						if (pk) {
479
							document.getElementById("div_write_1").hidden = true;
480
							document.getElementById("div_write_2").hidden = false;
481
482
							document.getElementById("write2_from").textContent = document.getElementById("write_from").value + "@" + ae.GetDomainEml();
483
							document.getElementById("write2_recv").textContent = document.getElementById("write_recv").value;
484
							document.getElementById("write2_pkey").textContent = sodium.to_hex(pk);
1 ignored issue
show
Bug introduced by
The variable sodium seems to be never declared. If this is a global, consider adding a /** global: sodium */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
485
486
							document.getElementById("write2_subj").textContent = document.getElementById("write_subj").value;
487
							document.getElementById("write2_rply").textContent = document.getElementById("write_rply").textContent;
488
							document.getElementById("write2_body").textContent = document.getElementById("write_body").value;
489
						} else {
490
							console.log("Failed lookup");
491
						}
492
					});
493
				break;
494
495
				case 2:
496
					const topk = (document.getElementById("write2_recv").textContent.indexOf("@") > 0) ? null : sodium.from_hex(document.getElementById("write2_pkey").textContent);
1 ignored issue
show
Bug introduced by
The variable sodium seems to be never declared. If this is a global, consider adding a /** global: sodium */ comment.

This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.

To learn more about declaring variables in Javascript, see the MDN.

Loading history...
497
					ae.Message_Create(document.getElementById("write_subj").value, document.getElementById("write_body").value, document.getElementById("write_from").value, document.getElementById("write_recv").value, document.getElementById("write_rply").textContent, topk, function(success) {
498
						if (success) {
499
							console.log("Sent ok");
500
						} else {
501
							console.log("Failed sending");
502
						}
503
					});
504
				break;
505
			}
506
		break;
507
	}
508
}
509
510
document.getElementById("btn_left").onclick = function() {
511
	tabs[tab].cur--;
512
	if (tabs[tab].cur === 0) this.disabled = true;
513
	if (tabs[tab].cur < tabs[tab].max) document.getElementById("btn_rght").disabled = false;
514
	updateTab();
515
	this.blur();
516
};
517
518
document.getElementById("btn_rght").onclick = function() {
519
	tabs[tab].cur++;
520
	if (tabs[tab].cur === tabs[tab].max) this.disabled = true;
521
	document.getElementById("btn_left").disabled = false;
522
	updateTab();
523
	this.blur();
524
};
525
526
const buttons = document.querySelector("#main1 > .top").getElementsByTagName("button");
527
for (let i = 0; i < buttons.length; i++) {
528
	buttons[i].onclick = function() {
529
		tab = i;
530
531
		for (let j = 0; j < buttons.length; j++) {
532
			document.querySelector("#main1 > .mid").children[j].hidden = (tab !== j);
533
			buttons[j].disabled = (tab === j);
534
		}
535
536
		document.getElementById("btn_left").disabled = (tabs[tab].cur === 0);
0 ignored issues
show
Bug introduced by
The variable tab is changed as part of the for loop for example by i on line 529. Only the value of the last iteration will be visible in this function if it is called after the loop.
Loading history...
537
		document.getElementById("btn_rght").disabled = (tabs[tab].cur === tabs[tab].max);
538
	};
539
}
540
541
function addressCreate(addr) {
542
	const btnN = document.getElementById("btn_address_create_normal");
543
	const btnS = document.getElementById("btn_address_create_shield");
544
	btnN.disabled = true;
545
	btnS.disabled = true;
546
547
	ae.Address_Create(addr, function(success1) {
548
		if (success1) {
549
			ae.Private_Update(function(success2) {
550
				addAddress(ae.GetAddressCount() - 1);
551
				if (addr !== "SHIELD") document.getElementById("txt_address_create_normal").value = "";
552
				updateAddressCounts();
553
554
				if (!success2) console.log("Failed to update the Private field");
555
556
				if (ae.GetAddressCountNormal() < ae.GetAddressLimitNormal(ae.GetUserLevel())) btnN.disabled = false;
557
				if (ae.GetAddressCountShield() < ae.GetAddressLimitShield(ae.GetUserLevel())) btnS.disabled = false;
558
			});
559
		} else {
560
			console.log("Failed to add address");
561
562
			if (ae.GetAddressCountNormal() < ae.GetAddressLimitNormal(ae.GetUserLevel())) btnN.disabled = false;
563
			if (ae.GetAddressCountShield() < ae.GetAddressLimitShield(ae.GetUserLevel())) btnS.disabled = false;
564
		}
565
	});
566
}
567
568
document.getElementById("btn_address_create_normal").onclick = function() {
569
	if (ae.GetAddressCountNormal() >= ae.GetAddressLimitNormal(ae.GetUserLevel())) return;
570
571
	const txtNewAddr = document.getElementById("txt_address_create_normal");
572
	if (!txtNewAddr.reportValidity()) return;
573
574
	addressCreate(txtNewAddr.value);
575
};
576
577
document.getElementById("btn_address_create_shield").onclick = function() {
578
	if (ae.GetAddressCountShield() >= ae.GetAddressLimitShield(ae.GetUserLevel())) return;
579
580
	addressCreate("SHIELD");
581
};
582
583
document.getElementById("txt_skey").onkeyup = function(event) {
584
	if (event.key === "Enter") {
585
		event.preventDefault();
586
		document.getElementById("btn_enter").click();
587
	}
588
};
589
590
document.getElementById("btn_enter").onclick = function() {
591
	const txtSkey = document.getElementById("txt_skey");
592
	if (!txtSkey.reportValidity()) return;
593
594
	const btn = this;
595
	btn.disabled = true;
596
	document.getElementById("txt_skey").style.background = "#233";
597
598
	ae.SetKeys(txtSkey.value, function(successSetKeys) {
599
		if (successSetKeys) {
600
			ae.Account_Browse(0, function(successBrowse) {
601
				if (successBrowse) {
602
					txtSkey.value = "";
603
					reloadInterface();
604
					document.getElementById("btn_updt").click();
605
				} else {
606
					console.log("Failed to enter");
607
					btn.disabled = false;
608
					document.getElementById("txt_skey").style.background = "#466";
609
					txtSkey.focus();
610
				}
611
			});
612
		} else {
613
			console.log("Invalid format for key");
614
			btn.disabled = false;
615
			document.getElementById("txt_skey").style.background = "#466";
616
			txtSkey.focus();
617
		}
618
	});
619
};
620
621
});
622